home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1998 / MacHack 1998.toast / Sessions / Completions / Demo Application / FileCopier.cp next >
Encoding:
Text File  |  1998-06-19  |  3.3 KB  |  120 lines  |  [TEXT/CWIE]

  1. // FileCopier.cp
  2.  
  3. #ifndef FileCopier_h
  4. #include "FileCopier.h"
  5. #endif
  6.  
  7. FileCopier::FileCopier()
  8.   : sequence( *this ),
  9.      pipe( Data( buffer + 4096 - uint32(buffer)%4096,
  10.                       sizeof(buffer) - 4096 ) )
  11.   {
  12.   }
  13.  
  14. Task *FileCopier::operator()( const FSSpec& source,
  15.                                         const FSSpec& destination )
  16.   {
  17.     sourceFile = &source;
  18.     destinationFile = &destination;
  19.     dataCopied = 0;
  20.     info.hFileInfo.ioFlLgLen = 0;
  21.     info.hFileInfo.ioFlRLgLen = 0;
  22.     return sequence.Start( Step( 0, &FileCopier::CreateDestination ) );
  23.   }
  24.  
  25. TaskStep<FileCopier> FileCopier::CreateDestination( bool, DeferredTaskTime )
  26.   {
  27.     return Step( create( *destinationFile ), &FileCopier::GetSourceInfo );
  28.   }
  29.  
  30. TaskStep<FileCopier> FileCopier::GetSourceInfo( bool, DeferredTaskTime )
  31.   {
  32.     return Step( info.Get( *sourceFile ), &FileCopier::SetDestinationInfo );
  33.   }
  34.  
  35. TaskStep<FileCopier> FileCopier::SetDestinationInfo( bool, DeferredTaskTime )
  36.   {
  37.     return Step( info.Set( *destinationFile ), &FileCopier::OpenDestinationData );
  38.   }
  39.  
  40. TaskStep<FileCopier> FileCopier::OpenDestinationData( bool, DeferredTaskTime )
  41.   {
  42.     if ( info.hFileInfo.ioFlLgLen == 0 )
  43.         return Step( 0, &FileCopier::OpenDestinationResources );
  44.  
  45.     return Step( open.OpenDataFork( *destinationFile, destinationPath ),
  46.                      &FileCopier::OpenSourceData );
  47.   }
  48.  
  49. TaskStep<FileCopier> FileCopier::OpenSourceData( bool, DeferredTaskTime )
  50.   {
  51.     return Step( open.OpenDataFork( *sourceFile, sourcePath ),
  52.                      &FileCopier::CopyData );
  53.   }
  54.  
  55. TaskStep<FileCopier> FileCopier::CopyData( bool, DeferredTaskTime )
  56.   {
  57.     pipe.Reset();
  58.     outStream.SetFile( destinationPath );
  59.     inStream.SetFile( sourcePath );
  60.     
  61.     inStream.SuggestNoCaching();
  62.     outStream.SuggestNoCaching();
  63.  
  64.     return Step( race( pumpIn( inStream, pipe.In() ),
  65.                              pumpOut( pipe.Out(), outStream ),
  66.                              1 ),
  67.                      &FileCopier::CloseSourceData );
  68.   }
  69.  
  70. TaskStep<FileCopier> FileCopier::CloseSourceData( bool, DeferredTaskTime )
  71.   {
  72.     return Step( close( sourcePath ), &FileCopier::CloseDestinationData );
  73.   }
  74.  
  75. TaskStep<FileCopier> FileCopier::CloseDestinationData( bool, DeferredTaskTime )
  76.   {
  77.     return Step( close( destinationPath ), &FileCopier::OpenDestinationResources );
  78.   }
  79.  
  80. TaskStep<FileCopier> FileCopier::OpenDestinationResources( bool, DeferredTaskTime )
  81.   {
  82.     if ( info.hFileInfo.ioFlRLgLen == 0 )
  83.         return Step( 0 );
  84.     
  85.     return Step( open.OpenResourceFork( *destinationFile, destinationPath ),
  86.                      &FileCopier::OpenSourceResources );
  87.   }
  88.  
  89. TaskStep<FileCopier> FileCopier::OpenSourceResources( bool, DeferredTaskTime )
  90.   {
  91.     return Step( open.OpenResourceFork( *sourceFile, sourcePath ),
  92.                      &FileCopier::CopyResources );
  93.   }
  94.  
  95. TaskStep<FileCopier> FileCopier::CopyResources( bool, DeferredTaskTime )
  96.   {
  97.     pipe.Reset();
  98.     dataCopied = info.hFileInfo.ioFlLgLen;
  99.     outStream.SetFile( destinationPath );
  100.     inStream.SetFile( sourcePath );
  101.     
  102.     inStream.SuggestNoCaching();
  103.     outStream.SuggestNoCaching();
  104.     
  105.     return Step( race( pumpIn( inStream, pipe.In() ),
  106.                              pumpOut( pipe.Out(), outStream ),
  107.                              1 ),
  108.                      &FileCopier::CloseSourceResources );
  109.   }
  110.  
  111. TaskStep<FileCopier> FileCopier::CloseSourceResources( bool, DeferredTaskTime )
  112.   {
  113.     return Step( close( sourcePath ), &FileCopier::CloseDestinationResources );
  114.   }
  115.  
  116. TaskStep<FileCopier> FileCopier::CloseDestinationResources( bool, DeferredTaskTime )
  117.   {
  118.     return Step( close( destinationPath ) );
  119.   }
  120.